package com.example.zhaoyulu.yunzhiapp;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.SearchView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import static android.widget.Toast.LENGTH_SHORT;

public class SearchActvity extends zylBaseActivity  implements SearchView.OnQueryTextListener{
    private String[] strs = new String[]{
            "java","python","html"
    };
    private ListView list;
    private SearchView searchView;
    private ArrayAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_actvity);
        list = (ListView) findViewById(R.id.list);
        list.setAdapter(new ArrayAdapter<String >(this,android.R.layout.simple_list_item_1,strs));
        adapter =  new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strs);




        list.setTextFilterEnabled(true);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                Toast.makeText(SearchActvity.this, strs[i], LENGTH_SHORT).show();
                gotoSearch("https://blog.csdn.net/u010837794/article/details/63251725");
            }
        });

        searchView =findViewById(R.id.sv);
        searchView.setIconifiedByDefault(false);
        searchView.setOnQueryTextListener(this);
        searchView.setSubmitButtonEnabled(true);
        searchView.setQueryHint("查找");

    }



    @Override
    public boolean onQueryTextSubmit(String s) {
        if (searchView != null) {
            // 得到输入管理对象
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm != null) {
                // 这将让键盘在所有的情况下都被隐藏，但是一般我们在点击搜索按钮后，输入法都会乖乖的自动隐藏的。
                imm.hideSoftInputFromWindow(searchView.getWindowToken(), 1); // 输入法如果是显示状态，那么就隐藏输入法
                gotoSearch("https://blog.csdn.net/u010837794/article/details/63251725");
            }
            searchView.clearFocus(); // 不获取焦点
        }

        return true;
    }

    public void gotoSearch(String str){
        Intent intent =new Intent(SearchActvity.this,webActivity.class);
        //用Bundle携带数据
        Bundle bundle=new Bundle();
        //传递name参数为tinyphp
        bundle.putString("url",str);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    @Override
    public boolean onQueryTextChange(String s) {
        if (TextUtils.isEmpty(s)){
            list.clearTextFilter();
        }else{
            adapter.getFilter().filter(s);
        }
        return true;
    }
}
